package com.sskj.mine.ui.activity;


import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.PwdForgetActivityPresenter;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.MINE_PWD_FORGET)//忘记密码
public class PwdForgetActivity extends BaseActivity<PwdForgetActivityPresenter> {

    @BindView(R2.id.etPhone)
    EditText etPhone;
    @BindView(R2.id.btNextStep)
    TextView btNextStep;
    @BindView(R2.id.viewPhone)
    View viewPhone;
    @BindView(R2.id.llPhone)
    LinearLayout llPhone;
    @BindView(R2.id.viewEmail)
    View viewEmail;
    @BindView(R2.id.llEmail)
    LinearLayout llEmail;
    @BindView(R2.id.btGetCheckCode)
    Button btGetCheckCode;
    @BindView(R2.id.etCode)
    EditText etCode;
    private boolean isPhone = true;
    private Disposable timeDis;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_pwd_forget;
    }

    @Override
    public PwdForgetActivityPresenter getPresenter() {

        return new PwdForgetActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle("忘记密码");
        ClickUtil.click(btGetCheckCode, () -> {
            if (CheckUtil.isMobile(etPhone.getText().toString())) {
                btGetCheckCode.setEnabled(false);
                mPresenter.sendCode(etPhone.getText().toString());
            } else if (CheckUtil.isEmail(etPhone.getText().toString())) {
                btGetCheckCode.setEnabled(false);
                mPresenter.sendEmailCode(etPhone.getText().toString());
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdForgetActivity0));
            }
        });
        ClickUtil.click(btNextStep, () -> {
            ARouter.getInstance().build(RConfig.MINE_PWD_RESET)
                    .withString(Constans.PHONE, etPhone.getText().toString())
                    .withString(Constans.CODE, etCode.getText().toString())
                    .navigation();
        });
        ClickUtil.click(llPhone, () -> {
            if (!isPhone) {
                isPhone = true;
                viewPhone.setVisibility(View.VISIBLE);
                viewEmail.setVisibility(View.INVISIBLE);
                etPhone.setHint("请输入手机号");
                etPhone.setText("");
                etPhone.setInputType(InputType.TYPE_CLASS_PHONE);
            }
        });
        ClickUtil.click(llEmail, () -> {
            if (isPhone) {
                isPhone = false;
                viewPhone.setVisibility(View.INVISIBLE);
                viewEmail.setVisibility(View.VISIBLE);
                etPhone.setHint("请输入邮箱号");
                etPhone.setText("");
                etPhone.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);


            }
        });
    }

    public void checkCode(String mobile) {
        ARouter.getInstance().build(RConfig.MINE_CODE_CHECK)
                .withString(Constans.PHONE, mobile)
                .navigation();
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime(this)
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        btGetCheckCode.setText(aLong + "s");
                    } else {
                        btGetCheckCode.setText("重新获取验证码");
                        btGetCheckCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        btGetCheckCode.setEnabled(true);
    }

}
